package org.mebusw.remote;

import org.mebusw.davincicode.GameManager;
import org.mebusw.davincicode.PlayerState;
import org.mebusw.vo.GameTable;

public class DavinciPlayService extends GamePlayService {

	public int refreshGame(String playerName, int gid) {
		return super.refreshGame(playerName, gid, GameTable.DAVINCI_CODE);

	}

	public boolean takeACard(String playerName, int gid) {
		GameManager gm = new GameManager();
		gm.loadGame(gid);
		int pid = gm.playerNameToID(playerName);
		if (pid == gm.getGt().getCurrPlayer()
				&& PlayerState.TAKING == gm.getPlayerManager()[pid].getPlayer()
						.getState()) {
			gm.onTake();
			gm.saveGame();
			refreshAllUI(gm.getGt(), playerName, gid, "ALL");
			return true;
		} else
			return false;
	}

	public boolean guessACard(String playerName, int gid, int cardId, int target) {
		GameManager gm = new GameManager();
		gm.loadGame(gid);
		int pid = gm.playerNameToID(playerName);
		if (pid == gm.getGt().getCurrPlayer()
				&& (PlayerState.FIRSTGUESSING == gm.getPlayerManager()[pid]
						.getPlayer().getState() || PlayerState.MOREGUESSING == gm
						.getPlayerManager()[pid].getPlayer().getState())) {
			gm.onGuess(cardId, target);
			gm.saveGame();
			refreshAllUI(gm.getGt(), playerName, gid, "ALL");
			return true;
		} else
			return false;
	}

	public boolean finish(String playerName, int gid, int newPos) {
		GameManager gm = new GameManager();
		gm.loadGame(gid);
		int pid = gm.playerNameToID(playerName);
		if (pid == gm.getGt().getCurrPlayer()
				&& (PlayerState.MOREGUESSING == gm.getPlayerManager()[pid]
						.getPlayer().getState() || PlayerState.INSERTING == gm
						.getPlayerManager()[pid].getPlayer().getState())) {
			gm.onFinish(newPos);
			gm.saveGame();
			refreshAllUI(gm.getGt(), playerName, gid, "ALL");
			return true;
		} else
			return false;
	}

}
